cmake_minimum_required(VERSION 3.10)
project(evdeploy)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-O3 -Wall")
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_INSTALL_PREFIX "/usr/local/evdeploy")
set(EVDEPLOY_VERSION "v1.1.0")          #evdeploy版本
set(EVDEPLOY_LIB_PATH "/usr/local/evdeploy/lib")    #evdeploy lib的目录

option(BUILD_TESTS "whether to build test" ON)
option(WITH_TENSORRT_RUNTIME "whether to build with tensorrt" OFF)       #是否启用tensorrt
option(WITH_ASCENDCL_RUNTIME "whether to build with ascendcl" OFF)      #是否启用acl
option(WITH_SOPHON_RUNTIME "whether to build with sophon sdk" OFF)         #是否启用sophon sdk
option(WITH_LYNXI_RUNTIME "whether to build with lynxi sdk" OFF)         #是否启用lynxi sdk

if(WITH_TENSORRT_RUNTIME)
    add_definitions("-DWITH_TENSORRT_RUNTIME")
endif()
if(WITH_ASCENDCL_RUNTIME)
    add_definitions("-DWITH_ASCENDCL_RUNTIME")
endif()
if(WITH_SOPHON_RUNTIME)
    set(OpenCV_DIR /opt/sophon/sophon-opencv-latest/lib/cmake/opencv4/)
    add_definitions("-DWITH_SOPHON_RUNTIME")
endif()
if(WITH_LYNXI_RUNTIME)
    add_definitions("-DWITH_LYNXI_RUNTIME")
endif()
# 获取cpu架构
set(ARCH_NAME "")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
    set(ARCH_NAME "x86_64")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
    set(ARCH_NAME "aarch64")
endif()

#添加cmake路径
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

#引入opencv
include(opencv)

#获取待编译的cpp文件路径
file(GLOB_RECURSE BASESRCS evdeploy/core/*.cpp)
file(GLOB_RECURSE UTILSRCS evdeploy/utils/*/*.cpp evdeploy/utils/*/*.c)
file(GLOB_RECURSE CVSRCS evdeploy/cv/*/*.cpp)
file(GLOB_RECURSE ALGOSRCS evdeploy/algo/*.cpp)

#所有cpp文件路径集合到BASESRCS上
list(APPEND BASESRCS ${UTILSRCS} ${CVSRCS} ${ALGOSRCS})
add_library(${PROJECT_NAME} SHARED ${BASESRCS}) #构建libevdeploy库
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,--disable-new-dtags,-rpath,${EVDEPLOY_LIB_PATH}") #设置lib依赖路径

configure_file(version_config.h.in ${CMAKE_BINARY_DIR}/generated/version_config.h)  #configure版本号头文件

target_include_directories(${PROJECT_NAME}
PUBLIC
    ${CMAKE_SOURCE_DIR}
    ${OpenCV_INCLUDE_DIRS}
PRIVATE
    ${CMAKE_BINARY_DIR}/generated/
)

target_link_libraries(${PROJECT_NAME}
PRIVATE
    ${OpenCV_LIBS}
    dl
    curl
    pthread
)

#引入evauth与runtime库
if(WITH_TENSORRT_RUNTIME)
    set(evauth_GIT_BRANCH "trt_${ARCH_NAME}")
    include(tensorrt)
    set(RUNTIME_DIR evdeploy/runtime/trt)
elseif(WITH_ASCENDCL_RUNTIME)
    set(evauth_GIT_BRANCH "acl_${ARCH_NAME}")
    include(ascendcl)
    set(RUNTIME_DIR evdeploy/runtime/ascend)
    target_link_libraries(${PROJECT_NAME}
    PRIVATE
        ${RUNTIME_LIBS}
    )
    target_include_directories(${PROJECT_NAME}
    PUBLIC
        ${RUNTIME_INCLUDE_DIRS}
    )
elseif(WITH_SOPHON_RUNTIME)
    set(evauth_GIT_BRANCH "sophon_${ARCH_NAME}")
    include(sophon)
    set(RUNTIME_DIR evdeploy/runtime/sophon)
elseif(WITH_LYNXI_RUNTIME)
    set(evauth_GIT_BRANCH "lynxi_${ARCH_NAME}")
    include(lynxi)
    set(RUNTIME_DIR evdeploy/runtime/lynxi)
endif()

# 链接runtime库
target_link_libraries(${PROJECT_NAME}
    PRIVATE
        ${RUNTIME_LIBS}
)

# 链接runtime头文件
target_include_directories(${PROJECT_NAME}
    PUBLIC
        ${RUNTIME_INCLUDE_DIRS}
)

# set(evauth_DIR /workspace/evauth/build/libevauth/)    #如何设置了该变量则从当前路径寻找libevauth库，否则通过Fetchcontext下拉
include(evauth)
add_subdirectory(${RUNTIME_DIR})

message(STATUS "evauth_LIBS:${evauth_LIBS}")
if(BUILD_TESTS)
    add_subdirectory(tests)
endif()

install(
  DIRECTORY ${CMAKE_SOURCE_DIR}/evdeploy
  DESTINATION ${CMAKE_INSTALL_PREFIX}/include
  FILES_MATCHING
  PATTERN "*.h"
)

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(FILES ${evauth_LIBS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(DIRECTORY ${evauth_CONFIG_DIR}/ DESTINATION ${CMAKE_INSTALL_PREFIX}/configs)